// pages/discuss/discuss.js
var app = getApp()
var db= wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    Type:"体育活动",
    iconsrc:"cloud://cj-0kz5u.636a-cj-0kz5u-1301703189/images/icon/体育活动.png",
    viewLeft : true,
    weixinNumber:'text',
    scrollHeight: 0,
    showModal:false,
    current　: 'left',
    datail:"省级立项大创项目招募新成员啦!\n本项目为新型职业农民教育平台，囊括了多方面助农业务和资源。项目建立一年半来， 曾多次获得丁颖杯创客杯等校级、创青春等省级创业大赛奖项。团队包含研究生、本科生，背靠成型创业公司,现因队内部分成员毕业，拟招募新成员。\n加入我们!让你对创业的梦想不再是渴望!创业之余，团队还能够给你提供学习、锻炼、",
    listData: [],
    listData_left: [],
    listData_mid: [],
    listData_right: [],
    FindTeam:[],
    FindTeammates:[],
    Detail:{
      
    },
   
    ev:{target:{dataset:{current:'right'}}}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this


    
  
    this.calcScrollHeight();

    db.collection('FindTeammates').get({
      success:function(res){
        that.setData({
            listData_left:res.data
        })
      }
    })
    db.collection('FindTeam').get({
      success:function(res){
        that.setData({
            listData_mid:res.data 
        })
      }
    })


    db.collection('user').where({
      _openid: 'app.globalData.openid' // 填入当前用户 openid
    }).get({
      success:function(res){
          that.data.FindTeam=res.data[0].publish.FindTeam,
          that.data.FindTeammates=res.data[0].publish.FindTeammates
        
            that.getmy()      
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handleCurrent(ev) {
    
    let current = ev.target.dataset.current;
    if (current == this.data.current) {
      return false;
    }

    if(current=='right'){
      this.setData({
        listData_right:this.data.listData_right
      })
    }
 
    this.setData({
      current: current,
    });
   
  },
  calcScrollHeight() {
    let that = this;
    let query = wx.createSelectorQuery().in(this);
    query.select('.top').boundingClientRect(function (res) {
      let topHeight = res.height;
      let screenHeight = wx.getSystemInfoSync().windowHeight;
      let scrollHeight = screenHeight - topHeight + 200;
      //这个70就是top部分上面的标题和电量这些
      that.setData({
        scrollHeight: scrollHeight
      })
    }).exec();
  },
  open:function (res){

    var index=res.target.dataset.index
    const current=this.data.current

    console.log(index)
    if(current=='left')
    {
     
      this.setData({
        Detail:this.data.listData_left[index].Team
      })

    }
    else if(current=='mid')
    {
      
      this.setData({
        Detail:this.data.listData_mid[index].FindTeam
      })
    }
    this.setData({
      showModal: true
    })
  },
  close:function(){
    wx.showToast({
      title: '已成功复制',
      duration:1000
      })
    this.setData({
      showModal: false
    })
    
  },
  gotoeditinfo:function(){
    wx.navigateTo({
      url: '/pages/Team/editInfo/editInfo',
    })
  },
  handleCopyText(ev) {

    var wxNum = ev.target.dataset.wxnum
    var that = this
    wx.setClipboardData({
      data: wxNum,
      success(res) {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              title: '复制成功'
            })
            
          }
        
        }),
          that.setData({
            showModal: false
          })
      }
    })
  },
  handletouch(){
    this.setData({
      showModal: false
    })
  },


  getmy:function(){
    var that= this
    var promiseArr = [];
    var FindTeam=that.data.FindTeam;
    var FindTeammates=that.data.FindTeammates;
    var i=0;
    var id="";

  
    for(i=0;i<FindTeam.length;i++)
    {
      promiseArr.push(new Promise((reslove, reject) => {
        id=FindTeam[i]
        db.collection("FindTeam").doc(id).get({
          success:function(res){          
            that.data.listData_right.push(res.data)
            reslove() 
          }
        })
      }));

    }
       

    for(i=0;i<FindTeammates.length;i++)
    {
      promiseArr.push(new Promise((reslove, reject) => {
        id=FindTeammates[i]
        db.collection("FindTeammates").doc(id).get({
          success:function(res){            
            that.data.listData_right.push(res.data)  
            reslove()        
          }
        })
      }));
        
    }
     
    Promise.all(promiseArr).then(res => {//等数组都做完后做then方法
      this.shuaxin()
    })
    },
    shuaxin:function()
    {
      console.log(this.data.listData_right)
      console.log("yes2")
      var TeamIndex = app.globalData.TeamIndex;
      if(TeamIndex==3){
        this.handleCurrent(this.data.ev)
        app.globalData.TeamIndex = 1
   }
    }
})
